;;; This file is part of guix-bavier.git
;;; Copyright © 2019 Eric Bavier <bavier@member.fsf.org>
;;; License: GPLv3+

(define-module (bavier packages openspades)
  #:use-module (guix packages)
  #:use-module (guix build-system cmake)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module (gnu packages audio)
  #:use-module (gnu packages cmake)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages curl)
  #:use-module (gnu packages fontutils)
  #:use-module (gnu packages gl)
  #:use-module (gnu packages imagemagick)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages sdl)
  #:use-module (gnu packages xiph)
  #:use-module (gnu packages xorg)
  #:use-module ((bavier licenses) #:prefix license:)
  #:use-module ((guix licenses) #:prefix license:))

(define-public openspades
  (let ((commit "cb89824f32a26692f789926c0f6c4248d70d6345")
	(revision "0"))
    (package
     (name "openspades")
     (version (git-version "0.1.3" revision commit))
     (source (origin
	       (method git-fetch)
	       (uri (git-reference
		     (url "https://github.com/yvt/openspades.git")
		     (commit commit)))
	       (file-name (git-file-name name version))
	       (sha256
		(base32
		 "0lhvpjb6nvgp71asqc6mq8y04ghqa5rkdlfh4ra3x72l9rwkl5gq"))))
     (build-system cmake-build-system)
     (native-inputs
      `(("pkg-config" ,pkg-config)))
     (inputs
      `(("curl" ,curl)
	("freealut" ,freealut)
	("freetype" ,freetype)
	("glew" ,glew)
	("imagemagick" ,imagemagick)
	("libxext" ,libxext)
	("openal" ,openal)
	("opus" ,opus)
	("opusfile" ,opusfile)
	("sdl" ,(sdl-union (list sdl2 sdl2-image)))
	("unzip" ,unzip)
	("zip" ,zip)

        ;; This package could probably not be upstreamed to Guix in its
        ;; current form, due to redistribution restrictions on some of the
        ;; audio and model assets in this bundle.  See e.g.
        ;; 
        ;;   https://github.com/yvt/openspades/issues/424
        ;;   https://github.com/yvt/openspades/blob/master/Resources/downloadpak.sh
        ;;   and "License" section of http://openspades.yvt.jp/
        ;;   
	("non-free-assets"
	 ,(origin
	    (method url-fetch)
	    (uri "https://github.com/yvt/openspades-paks/releases/download/r33/OpenSpadesDevPackage-r33.zip")
	    (file-name "OpenSpadesDevPackage-r33.zip")
	    (sha256
	     (base32
	      "1bd2fyn7mlxa3xnsvzj08xjzw02baimqvmnix07blfhb78rdq9q9"))))))
     (home-page "http://openspades.yvt.jp/")
     (arguments
      `(#:tests? #f			;no tests to run
	#:configure-flags (list
			   ;; Configuration assumes opus headers are
			   ;; installed in the same place as opusfile
			   ;; headers, which they aren't, so amend the
			   ;; search path.
			   (string-append "-DCMAKE_CXX_FLAGS=-I"
					  (assoc-ref %build-inputs "opus")
					  "/include/opus")
			   ;; The openal libraries are dynamically
			   ;; loaded, so make sure dlopen knows where
			   ;; to find them.
			   (string-append "-DCMAKE_EXE_LINKER_FLAGS=-Wl,-rpath,"
					  (assoc-ref %build-inputs "openal")
					  "/lib")
			   ;; Otherwise installs to "games", which
			   ;; will not be in PATH.
			   "-DOPENSPADES_INSTALL_BINARY:STRING=bin")
	#:phases (modify-phases %standard-phases
		   (add-after 'unpack 'stage-assets
		     (lambda* (#:key inputs #:allow-other-keys)
		       ;; The make rule looks for the .zip to decide
		       ;; whether to run the "downloadpak.sh" script.
		       ;; But this script also unzips and moves a few
		       ;; files around that the install rules look
		       ;; for.  So have that script copy the "pak" out
		       ;; of the store instead of wget'ing.
		       (substitute* "Resources/downloadpak.sh"
			 (("^wget.*" line)
			  (string-append "#" line
					 "cp " (assoc-ref inputs "non-free-assets") " "
					 "OpenSpadesDevPackage-r33.zip\n\n")))
		       #t)))))
     (synopsis "Voxel first-person shooter game")
     (description "OpenSpades is a clone of Ace of Spades 0.75, which
is a free online first-person shooter created by Ben Aksoy, featuring
fully destructible terrain and plenty of game modes (including the
well-known Capture the Flag) created by the community.")
     (license
      (list license:gpl3+
	    (license:non-redistributable-assets "http://openspades.yvt.jp/"))))))
